#!/usr/bin/bash
dostuff(){
  sinks=$(pactl list sink-inputs  \
          | grep -e "Sink Input" \
          | sed 's/Sink Input #//')
  names=$(pactl list sink-inputs \
          | sed 's/"//g' \
          | sed 's/application-name://'  \
          | grep -e "application.name" \
          | sed 's/application.name = //' \
          | sed 's/\t\t//' )
  vols=$(pactl list sink-inputs  \
          | grep -e "Volume:" \
          | sed 's/Volume: front-left: //' \
          | sed 's/front-right: //' \
          | sed 's/front-right: //' \
          | tr " " "\n" \
          | grep -e % \
          | sed 's/%//g' )
  binaries=$(pactl list sink-inputs \
          | grep -e 'application.process.binary' -e 'application.icon_name' \
          | sed 's/\t//g' \
          | sed 's/application.process.binary = //g' \
          | sed 's/application.icon_name = //g' \
          | sed 's/"//g' )
  
  # echo "-=-=-=-=-=-=-=-=- Debug: Raw value -=-=-=-=-=-=-=-=-"
  # echo "$sinks"
  # echo "$names"
  # echo "$vols"
  
  IFS=$'\n'
  sinkarr=($(echo "$sinks"))
  namearr=($(echo "$names"))
  volarr=($(echo "$vols"))
  iconarr=($(echo "$binaries"))

  # Get icons
  for i in "${!iconarr[@]}"; do
    iconarr[$i]=$(geticons -t "$(gsettings get org.gnome.desktop.interface icon-theme | sed "s/'//g")" ${iconarr[i]} | head -n 1)
  done
  
  # Functions
  sanitize() {
      echo "$1" | sed 's/"/\"/g'
  }
  
  geticon() {
  #     notify-send "${volarr[$((i*2))]}"
      if [ "${volarr[$(($1*2))]}" -eq "0" ]; then
          echo ""
      else 
          echo ""
      fi
  }
  
  # Print final JSON
  printf '['
  for i in "${!sinkarr[@]}"; do
      if [ $i -ne 0 ]; then
          printf ', '
      fi
      echo -n '{''"sink": "'"${sinkarr[$i]}"'", "name": "'"${namearr[$i]}"'", "volume": "'"${volarr[$((i*2))]}"'"}'
    #   echo -n '{''"sink": "'"${sinkarr[$i]}"'", "icon": "'"${iconarr[$i]}"'", "name": "'"${namearr[$i]}"'", "volume": "'"${volarr[$((i*2))]}"'"}'
  done
  printf ']\n'
  
  # echo "$sinks"
  # echo "$names"
  # echo "$vols"
}


cd ~/.config/eww
# dostuff
scripts/audioapps

if [ "$1" == "--once" ]; then
  exit 0
else
    pactl subscribe | rg --line-buffered "on sink" | while read -r _; do
        scripts/audioapps
    done
fi